from enum import Enum
from app.lib.sms import Sms
from app.settings.config import SMS_SIGN, SMS_APP_ID, SMS_SECRET_KEY, SMS_SECRET_ID, SMS_TEMPLATE

sms_box = Sms({
    'secret_id': SMS_SECRET_ID,
    'secret_key': SMS_SECRET_KEY,
    'app_id': SMS_APP_ID,
    'sms_sign': SMS_SIGN,
})

sms_template = Enum('短信模板', (SMS_TEMPLATE))


async def sms_send(template_config, mobiles=None, params=None):
    """
    发送短信
        使用方法：
            await sms_send(sms_template.code.value, mobiles=['18811216239', params=['1', '2']])
    :param template_config: 模板配置，格式：{"id": "585163", "len": 2}
    :param mobiles:
    :param params:
    :return:
    """
    if params is None:
        params = []
    if mobiles is None:
        mobiles = []
    if not mobiles:
        return 1, '手机号列表为空'
    template_id = template_config.get('id')
    template_len = template_config.get('len', 0)
    if not template_id:
        return 1, '模板编号不存在'
    if template_len != params:
        return 1, f'参数个数与模板配置不一致：{template_len}个'

    sms_box.send(template_id, mobiles=mobiles, params=params)
